let vm = new Vue({
    el: '#app',
    data: {
        user: {},
        password2: "",
        tag: false
    },
    methods: {
        checkEmail: function (e) {
            if (this.user.email != undefined && this.user.email != "") {
                axios({
                    url: `/user/checkEmail/${this.user.email}`
                }).then(res => {
                    if (res.data.flag) {
                        this.tag = true;
                        $(e.target).removeClass('input-error');
                    } else {
                        this.tag = false;
                        layer.msg('邮箱已被绑定');
                        $(e.target).addClass('input-error');
                    }
                });
            }
        },
        checkUsername: function (e) {
            if (this.user.username != undefined && this.user.username != "") {
                axios({
                    url: `/user/checkUsername/${this.user.username}`
                }).then(res => {
                    if (res.data.flag) {
                        this.tag = true;
                        $(e.target).removeClass('input-error');
                    } else {
                        this.tag = false;
                        layer.msg('用户已被注册');
                        $(e.target).addClass('input-error');
                    }
                });
            }
        },
        checkPassword: function (e) {
            if (this.user.password != undefined && this.user.password != "" && this.password2 != "") {
                if (this.user.password === this.password2) {
                    this.tag = true;
                    $(e.target).removeClass('input-error');
                } else {
                    this.tag = false;
                    layer.msg('密码不一致');
                    $(e.target).addClass('input-error');
                }
            }
        },
        register: function () {
            if (this.tag) {
                if (this.user.username != "" && this.user.email != "" && this.password2 != "" && this.user.password != "") {
                    axios({
                        method: 'post',
                        url: 'user/register',
                        data: this.user
                    }).then(res => {
                        if (res.data.flag) {
                            layer.msg(res.data.message + ",两秒后跳回登录页面。");
                            setInterval(() => {
                                location.href = "/index.html"
                            }, 2000);
                        } else {
                            layer.msg(res.data.message);
                        }

                    });
                } else {
                    layer.msg("信息有误");
                }
            } else {
                layer.msg("信息有误");
            }
        }
    }
})